package com.jin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jin.bean.Gate;
import com.jin.common.ResponseResult;
import com.jin.service.GateService;
import com.jin.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created on 2025/5/14.
 * @author Zhongyuan Jin
 */
@RestController
@RequestMapping("/gate")
public class GateController {

    @Autowired
    private GateService gateService;

    //获取全部门岗信息
    @GetMapping("/all")
    public ResponseResult<List<Gate>> getAllGates() {
        List<Gate> gates = gateService.list();
        return ResponseResult.success("获取全部门岗信息", gates);
    }

    //查询所有门岗信息（支持分页）
    @GetMapping("/list")
    public ResponseResult<List<Gate>> list(@RequestParam(defaultValue = "1") int pageNum,
                                           @RequestParam(defaultValue = "10") int pageSize) {
        Page<Gate> page = new Page<>(pageNum, pageSize);

        List<Gate> records = gateService.page(page).getRecords();
        long total = gateService.page(page).getTotal();
        return ResponseResult.success("查询所有门岗信息（支持分页）",records,total);
    }

    //根据 ID 查询门岗信息
    @GetMapping("/{id}")
    public ResponseResult<Gate> getById(@PathVariable Integer id) {
        Gate gate = gateService.getById(id);
        if (gate == null) {
            return ResponseResult.fail("未找到该门岗",  null);
        }
        return ResponseResult.success("找到该门岗",gate);
    }

    //新增门岗信息
    @PostMapping
    public ResponseResult<Boolean> add(@RequestBody Gate gate) {
        boolean result = gateService.save(gate);
        return new ResponseResult<>(result ? 0 : 1, result ? "添加成功" : "添加失败", result, null);
    }

    //更新门岗信息
    @PutMapping
    public ResponseResult<Boolean> update(@RequestBody Gate gate) {
        // 添加调试日志
        System.out.println("接收到的门岗更新数据: " + gate);
        boolean result = gateService.updateById(gate);
        return new ResponseResult<>(result ? 0 : 1, result ? "更新成功" : "更新失败", result, null);
    }

    //删除门岗信息
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> delete(@PathVariable Integer id) {
        boolean result = gateService.removeById(id);
        return new ResponseResult<>(result ? 0 : 1, result ? "删除成功" : "删除失败", result, null);
    }

    //启用门岗
    @PutMapping("/enable/{id}")
    public ResponseResult<Boolean> enable(@PathVariable Integer id) {
        Gate gate = gateService.getById(id);
        if (gate == null) {
            return ResponseResult.fail("未找到该门岗",  null);
        }
        gate.setIsEnable(1); // 启用
        boolean result = gateService.updateById(gate);
        return new ResponseResult<>(result ? 0 : 1, result ? "启用成功" : "启用失败", result, null);
    }

    //禁用门岗
    @PutMapping("/disable/{id}")
    public ResponseResult<Boolean> disable(@PathVariable Integer id) {
        Gate gate = gateService.getById(id);
        if (gate == null) {
            return ResponseResult.fail("未找到该门岗",  null);
        }
        gate.setIsEnable(0); // 禁用
        boolean result = gateService.updateById(gate);
        return new ResponseResult<>(result ? 0 : 1, result ? "禁用成功" : "禁用失败", result, null);
    }

    //按名称模糊查询
    @GetMapping("/search")
    public ResponseResult<List<Gate>> searchByName(@RequestParam String gateName) {
        List<Gate> gates = gateService.list(new QueryWrapper<Gate>().like("gate_name", gateName));
        return ResponseResult.success( "按名称模糊查询成功", gates);
    }
}
